/*
 * @(#)Constraint.java
 */
package jp.ac.kobe_u.cs.cream;

import jp.ac.kobe_u.cs.cream.*;
import java.util.*;

/**
 * An abstract class for constraints.
 * A constraint is a component of a {@linkplain Network constraint network}.
 * See {@link Network} for example programs to construct constraints and
 * add them to a constraint network.
 * @see	Network
 * @since 1.0
 * @version 1.0, 01/31/03
 * @author Naoyuki Tamura (tamura@kobe-u.ac.jp)
 */
public abstract class Constraint {
    private Network network;
    private int index = -1;

    /**
     * Sole constructor.
     * (for invocation by subclass constructors, typically implicit)
     */
    protected Constraint(Network net) {
	network = net;
	network.add(this);
    }

    protected Network getNetwork() {
	return network;
    }

    protected void setIndex(int i) {
	index = i;
    }

    protected int getIndex() {
	return index;
    }

    /**
     * Creates a copy of this constraint for a new network <tt>net</tt>.
     * @return the copy of this constraint
     */
    protected abstract Constraint copy(Network net);

    protected void clearCondition() {
    }

    protected void setCondition(Condition condition) {
    }

    protected Condition extractCondition() {
	return null;
    }

    protected abstract boolean isModified();

    protected abstract boolean satisfy(Trail trail);

    protected static Variable copy(Variable v0, Network net) {
	int j = v0.getIndex();
	return net.getVariable(j);
    }

    protected static Variable[] copy(Variable v0[], Network net) {
	Variable v[] = new Variable[v0.length];
	for (int i = 0; i < v0.length; i++) {
	    int j = v0[i].getIndex();
	    v[i] = net.getVariable(j);
	}
	return v;
    }

    protected static boolean isModified(Variable v[]) {
	for (int i = 0; i < v.length; i++) {
	    if (v[i].isModified())
		return true;
	}
	return false;
    }

    protected static String toString(Variable v[]) {
	String s = "";
	if (v != null) {
	    String delim = "";
	    for (int i = 0; i < v.length; i++) {
		s += delim + v[i];
		delim = ",";
	    }
	}
	return "{" + s + "}";
    }

    protected static String toString(int a[]) {
	String s = "";
	if (a != null) {
	    String delim = "";
	    for (int i = 0; i < a.length; i++) {
		s += delim + a[i];
		delim = ",";
	    }
	}
	return "{" + s + "}";
    }
}

